# -*- coding: utf-8 -*-
__author__ = 'lztkdr'
__date__ = '2020/11/27 16:44'

"""
    迭代器计算费布纳奇数列,  0 1 1 2 3 5 某一项的值等于前两项的和
"""


class Fib(object):

	def __init__(self, num):
		# 计算次数
		self.num = num
		# 当前下标
		self.index = 0

		self.num1 = 0
		self.num2 = 1

	def __iter__(self):
		return self

	def __next__(self):
		if self.index < self.num:
			temp = self.num1

			# 同时交换两个变量的简写
			# self.num1 = self.num2
			# self.num2 = self.num1 +  self.num2

			self.num1, self.num2 = self.num2, (self.num1 + self.num2)
			self.index += 1
			return temp
		else:
			raise StopIteration


def testFib():
	for i in Fib(10):
		print(i)

	# 可迭代对象也可以用在list(迭代器),或者tuple(迭代器)并不只是for循环中
	print(list(Fib(10)))

testFib()